package com.hw.dagl.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hw.dagl.model.Dagl_T_Audit;
import com.hw.dagl.model.Sys_Role;
import com.hw.dagl.model.Sys_User;
import com.hw.dagl.service.IAuditService;
import com.hw.dao.utils.ControllerUtil;
import com.hw.dao.utils.Page;

/**
 * @author Administrator
 * 审核信息
 */
@Controller
public class AuditController extends BaseController{
	
    
    @Autowired
    private IAuditService auditService;
    
    /**
     * 跳转到权限申请页面
     */
    @RequestMapping(method=RequestMethod.GET)
    public String qxsq(HttpServletResponse response){
        return "yxsh/qxsq";
    }
    
    /**
     * 跳转到权限审核页面
     */
    @RequestMapping(method=RequestMethod.GET)
    public String qxsh(HttpServletResponse response){
        return "yxsh/qxsh";
    }
	
	/**
	 * 查询申请信息
	 */
    @RequestMapping(method=RequestMethod.POST)
    public void queryAuditByPage(HttpServletResponse response,HttpServletRequest request,Page<Dagl_T_Audit> page,Dagl_T_Audit dagl_T_Audit,String startTime,String endTime,String operatorType){
        Sys_User user=(Sys_User)request.getSession().getAttribute("user");//用户信息
        Sys_Role sysRole=(Sys_Role)request.getSession().getAttribute("role");//角色信息
        String yhdh="";//用户编号
        if(null!=user){
            yhdh=user.getYhdh();
            dagl_T_Audit.setApplicant(yhdh);
        }
        Page<Dagl_T_Audit> daglTAudits=auditService.queryAuditByPage(page, dagl_T_Audit, startTime, endTime,sysRole,operatorType);
        ControllerUtil.renderJson(response, daglTAudits, "encoding:UTF-8","no-cache:false");
    }
    
    /**
     * 通过applyNo查询申请信息
     */
    @RequestMapping(method=RequestMethod.POST)
    public void queryAuditByApplyNo(HttpServletResponse response,Page<Dagl_T_Audit> page,Dagl_T_Audit dagl_T_Audit){
        dagl_T_Audit=auditService.queryAuditByApplyNo(dagl_T_Audit);
        ControllerUtil.renderJson(response, dagl_T_Audit, "encoding:UTF-8","no-cache:false");
    }
    
    //新增权限申请信息
    @RequestMapping(method=RequestMethod.POST)
    public void addAudit(HttpServletResponse response,HttpServletRequest request,Dagl_T_Audit dagl_T_Audit){
        Sys_User user=(Sys_User)request.getSession().getAttribute("user");
        String yhdh="";//用户编号
        String bmbh="";//部门编号
        String bmmc="";//部门名称
        if(null!=user){
            yhdh=user.getYhdh();
            bmbh=user.getGlbm();
            bmmc=user.getBmmc();
        }
        dagl_T_Audit.setApplicant(yhdh);
        dagl_T_Audit.setApplicantBmbm(bmbh);
        dagl_T_Audit.setApplicantBmmc(bmmc);
        Dagl_T_Audit tempDagl_T_Audit=auditService.queryAuditByHphm(dagl_T_Audit);
        int num=0;
        String resCode="00";//00 失败   01 成功  02 重复申请
        if(null==tempDagl_T_Audit){
            num=auditService.addAudit(dagl_T_Audit);
        }else{
            resCode="02";
        }
        
        if(num==1){
            resCode="01";//成功
        }
        ControllerUtil.renderJson(response, resCode, "encoding:UTF-8","no-cache:false");
    }
    
    
    //权限判断(通过hphm和hpzl判断当前档案数据是否已审核)
    @RequestMapping(method=RequestMethod.POST)
    public void queryAuditInfo(HttpServletResponse response,HttpServletRequest request,Dagl_T_Audit dagl_T_Audit){
        Sys_User user=(Sys_User)request.getSession().getAttribute("user");
        String yhdh="";//用户编号
        if(null!=user){
            yhdh=user.getYhdh();
        }
        dagl_T_Audit.setApplicant(yhdh);
        List<Dagl_T_Audit> dagl_T_Audits=auditService.queryAuditByList(dagl_T_Audit);
        ControllerUtil.renderJson(response, dagl_T_Audits, "encoding:UTF-8","no-cache:false");
    }
    
    //审核权限申请信息
    @RequestMapping(method=RequestMethod.POST)
    public void auditApply(HttpServletResponse response,HttpServletRequest request,Dagl_T_Audit dagl_T_Audit){
        Sys_User user=(Sys_User)request.getSession().getAttribute("user");
        String yhdh="";//用户编号
        String bmbh="";//部门编号
        String bmmc="";//部门名称
        if(null!=user){
            yhdh=user.getYhdh();
            bmbh=user.getGlbm();
            bmmc=user.getBmmc();
        }
        dagl_T_Audit.setAuditor(yhdh);
        dagl_T_Audit.setAuditorBmbm(bmbh);
        dagl_T_Audit.setAuditorBmmc(bmmc);
        int num=0;
        String resCode="00";//00 审核失败   01 审核成功 
        if(null!=bmbh){
        	num=auditService.editAudit(dagl_T_Audit);
        	resCode="02";
        }
        
       
        if(num>0){
            resCode="01";
        }
        ControllerUtil.renderJson(response, resCode, "encoding:UTF-8","no-cache:false");
    }
    
    //更新审核权限申请信息(把当前申请信息变成无效信息)
    @RequestMapping(method=RequestMethod.POST)
    public void editAuditFlag(HttpServletResponse response,HttpServletRequest request,Dagl_T_Audit dagl_T_Audit){
        Sys_User user=(Sys_User)request.getSession().getAttribute("user");
        int num = auditService.updateAuditFlag(dagl_T_Audit, user);
        ControllerUtil.renderJson(response, num, "encoding:UTF-8","no-cache:false");
    }
    
    
	
	
}
